﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Microsoft.JCI.Bll;
using Microsoft.JCI.Common.Entity;
using System.Collections.Generic;
using Microsoft.JCI.Common.Utility;

namespace Microsoft.JCI.Portal.Dispatch
{
    public partial class BranchReDispatch : BasePage
    {
        public override bool IsCheckAccessible
        {
            get
            {
                return false;
            }
        }
        public Guid DispatchFormId
        {
            get
            {
                if (Request.QueryString["DispatchFormId"] != null)
                {
                    return new Guid(Request.QueryString["DispatchFormId"]);
                }
                else
                    return Guid.Empty;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["DispatchFormId"] != null)
                {
                    LoadData();
                }
            }
        }

        private void LoadData()
        {
            DispatchBll patchbll = new DispatchBll();
            DisDispatchFormEntity entity = patchbll.Get<DisDispatchFormEntity>(DispatchFormId);
            if (entity == null)
            {
                btnSave.Enabled = false;
                return;
            }

            if (entity.AllocatedBranch != "")
            {
                MasterDataBll mdBll = new MasterDataBll();
                List<SysCompanyEntity> list = mdBll.GetCompanyListFromCacheByParentCode(entity.AllocatedBranch);
                ddlVendor.DataSource = list;
                ddlVendor.DataTextField = "Name";
                ddlVendor.DataValueField = "CompanyCode";
                ddlVendor.DataBind();
            }
            ddlVendor.SelectedValue = entity.AllocatedVendor;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (ddlVendor.SelectedValue == "")
            {
                ClientScript.RegisterStartupScript(this.GetType(), "err", "alert('请选择供应商!');", true);
                return;
            }
            DispatchBll patchbll = new DispatchBll();
            DisDispatchFormEntity entity = patchbll.Get<DisDispatchFormEntity>(DispatchFormId);
            if (entity == null) return;
            entity.AllocatedVendor = ddlVendor.SelectedValue;
            entity.FormStatus = (int)Enumerator.FormStatus.Dispatched;
            entity.DispatchStatus = (int)Enumerator.DispatchStatus.DispatchedVendor;

            patchbll.Update(entity);

            ClientScript.RegisterStartupScript(this.GetType(), "success", "alert('保存成功!');CloseWindow();", true);

        }
    }
}
